package com.hjm.project.auditdata.auditdata.controller;

import java.util.List;

import com.hjm.common.utils.security.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.auditdata.auditdata.domain.Auditdata;
import com.hjm.project.auditdata.auditdata.service.IAuditdataService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 清算数据Controller
 * 
 * @author LOVE
 * @date 2020-11-27
 */
@Controller
@RequestMapping("/auditdata/auditdata")
public class AuditdataController extends BaseController
{
    private String prefix = "auditdata/auditdata";

    @Autowired
    private IAuditdataService auditdataService;

    @RequiresPermissions("auditdata:auditdata:view")
    @GetMapping()
    public String auditdata()
    {
        return prefix + "/auditdata";
    }


    @GetMapping("/auditdataRecord")
    public String auditdataRecord()
    {
        return prefix + "/auditdataRecord";
    }


    /**
     * 查询清算数据列表
     */
    @RequiresPermissions("auditdata:auditdata:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Auditdata auditdata)
    {
        startPage();
        List<Auditdata> list = auditdataService.selectAuditdataList(auditdata);

        return getDataTable(list);
    }

    @PostMapping("/getList2")
    @ResponseBody
    public TableDataInfo getList2(Auditdata auditdata)
    {
        if (ShiroUtils.isAgent()) {
            auditdata.setLoginAgentNum(ShiroUtils.getLoginAgentNum());
        }
        startPage();
        List<Auditdata> list = auditdataService.getList2(auditdata);
        return getDataTable(list);
    }

    /**
     * 导出清算数据列表
     */
    @RequiresPermissions("auditdata:auditdata:export")
    @Log(title = "清算数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Auditdata auditdata)
    {
        List<Auditdata> list = auditdataService.selectAuditdataList(auditdata);
        ExcelUtil<Auditdata> util = new ExcelUtil<Auditdata>(Auditdata.class);
        return util.exportExcel(list, "auditdata");
    }

    /**
     * 新增清算数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存清算数据
     */
    @RequiresPermissions("auditdata:auditdata:add")
    @Log(title = "清算数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Auditdata auditdata)
    {
        return toAjax(auditdataService.insertAuditdata(auditdata));
    }

    /**
     * 修改清算数据
     */
    @GetMapping("/edit/{serial}")
    public String edit(@PathVariable("serial") String serial, ModelMap mmap)
    {
        Auditdata auditdata = auditdataService.selectAuditdataById(serial);
        mmap.put("auditdata", auditdata);
        return prefix + "/edit";
    }

    /**
     * 修改保存清算数据
     */
    @RequiresPermissions("auditdata:auditdata:edit")
    @Log(title = "清算数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Auditdata auditdata)
    {
        return toAjax(auditdataService.updateAuditdata(auditdata));
    }

    /**
     * 删除清算数据
     */
    @RequiresPermissions("auditdata:auditdata:remove")
    @Log(title = "清算数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(auditdataService.deleteAuditdataByIds(ids));
    }


    @Log(title = "分润补录", businessType = BusinessType.DELETE)
    @PostMapping( "/saveUpdateFr")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveUpdateFr(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            auditdataService.updateFr(s);
        }
        return success();
    }

}
